package com.system.connect.springboot_flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.connect.springboot_flowers.dto.PrivatemessagesDto;
import com.system.connect.springboot_flowers.entity.Privatemessages;
import com.system.connect.springboot_flowers.entity.Users;
import com.system.connect.springboot_flowers.service.IPrivatemessagesService;
import com.system.connect.springboot_flowers.service.IUsersService;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 私信表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/privatemessages")
public class PrivatemessagesController {

    @Autowired
    private IPrivatemessagesService privatemessagesService;

    @Autowired
    private IUsersService usersService;

    /**
     * 根据接收者ID获取私信列表
     * @param receiverId
     * @return
     */

    @Transactional
    @GetMapping("/{receiverId}") // 确保路径与前端请求匹配
    public R getPostDetail(@PathVariable("receiverId") Integer receiverId) {

        Integer senderId = Integer.valueOf(TokenUtils.getUser());
        QueryWrapper<Privatemessages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id", receiverId);
        queryWrapper.eq("sender_id", senderId);
        queryWrapper.or();
        queryWrapper.eq("receiver_id", senderId);
        queryWrapper.eq("sender_id", receiverId);
        queryWrapper.orderByAsc("created_at");

        ArrayList<Privatemessages> privatemessages = (ArrayList<Privatemessages>) privatemessagesService.list(queryWrapper);
        ArrayList<PrivatemessagesDto> privatemessagesDtos = new ArrayList<>();

        for (Privatemessages privatemessage : privatemessages) {

            Users receiver = usersService.getById(privatemessage.getReceiverId());
            Users sender = usersService.getById(privatemessage.getSenderId());
            PrivatemessagesDto privatemessagesDto = new PrivatemessagesDto();
            privatemessagesDto.setContent(privatemessage.getContent());
            privatemessagesDto.setCreatedAt(LocalDateTime.parse(privatemessage.getCreatedAt().toString()));
            privatemessagesDto.setReceiverAvatarUrl(receiver.getAvatarUrl());
            privatemessagesDto.setReceiverNickname(receiver.getUsername());
            privatemessagesDto.setReceiverId(receiver.getUserId());
            privatemessagesDto.setSenderAvatarUrl(sender.getAvatarUrl());
            privatemessagesDto.setSenderNickname(sender.getUsername());
            privatemessagesDto.setSenderId(sender.getUserId());
            privatemessagesDtos.add(privatemessagesDto);
        }

        return R.success(privatemessagesDtos);

    }

    /**
     * 添加私信
     */
    @PostMapping("/create")
    public R create(@RequestBody PrivatemessagesDto dto) {
        Privatemessages privatemessages = new Privatemessages();
        privatemessages.setContent(dto.getContent());
        privatemessages.setReceiverId(dto.getReceiverId());
        privatemessages.setSenderId(Integer.valueOf(TokenUtils.getUser())); // 假设这是发送者ID
        privatemessagesService.save(privatemessages);

        return R.success("私信成功");
    }

}
